package com.ljt.Map;

import Creature.Monster;
import Creature.Person;
import com.ljt.Weapon.BowandArrow;
import com.ljt.Weapon.Knife;
import Creature.MonsterSet;
import com.sun.deploy.security.SelectableSecurityManager;

import javax.jws.Oneway;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;


/**
 * @description:
 * @author: 28437
 * @date: Created in 2020/5/31 8:41
 * @version: 1.0
 * @modified By:
 */
public class Sense {
    private UI ui = new UI();
    private Person person = new Person();
    private Monster wolf1 = new Monster();
    private Monster wolf2 = new Monster();
    private Monster Gnome1 = new Monster();
    private Monster Gnome2 = new Monster();
    private Monster Skeleton = new Monster();
    private MonsterSet monsterSet = new MonsterSet();
    private PlaceSet placeSet = new PlaceSet();
    private Place house, forest, plain, castle, marsh;

    public void initSense() {

    }

    public void initPlace() {
        house = new Place("小屋");
        forest = new Place("森林");
        plain = new Place("平原");
        castle = new Place("城堡");
        marsh = new Place("沼泽");

        house.setExit("west", plain);
        house.setExit("east", forest);
        house.setExit("south", marsh);
        placeSet.getPlaces().add(house);

        forest.setExit("south", marsh);
        forest.setExit("west", house);
        forest.setExit("north", plain);
        placeSet.getPlaces().add(forest);

        plain.setExit("south", forest);
        plain.setExit("east", house);
        plain.setExit("north", marsh);
        placeSet.getPlaces().add(plain);

        marsh.setExit("north", house);
        marsh.setExit("south", plain);
        placeSet.getPlaces().add(marsh);

        person.setCurrentPlace(house);
    }

    public void move(Person person, String direction) {
        int Flag = 0;
        for (String exit : person.getCurrentPlace().getExits().keySet()
        ) {
            if (exit.equals(direction)) {
                Flag = 1;
                person.setCurrentPlace(person.getCurrentPlace().getExits().get(exit));
                break;
            }
        }
        if (Flag == 0) {
            ui.noWay();
        }
    }

    public void initPerson() {
        person.setId("Adventurer");
        person.setDescription("安娜");
        person.setHpValue(500);
        person.setCurrentWeapon(new Knife("刀", "刀", -50));

    }

    public UI getUi() {
        return ui;
    }

    public void setUi(UI ui) {
        this.ui = ui;
    }

    public Person getPerson() {
        return person;
    }

    public void setPerson(Person person) {
        this.person = person;
    }

    public Monster getWolf1() {
        return wolf1;
    }

    public void setWolf1(Monster wolf1) {
        this.wolf1 = wolf1;
    }

    public Monster getWolf2() {
        return wolf2;
    }

    public void setWolf2(Monster wolf2) {
        this.wolf2 = wolf2;
    }

    public Monster getGnome1() {
        return Gnome1;
    }

    public void setGnome1(Monster gnome1) {
        Gnome1 = gnome1;
    }

    public Monster getGnome2() {
        return Gnome2;
    }

    public void setGnome2(Monster gnome2) {
        Gnome2 = gnome2;
    }

    public Monster getSkeleton() {
        return Skeleton;
    }

    public void setSkeleton(Monster skeleton) {
        Skeleton = skeleton;
    }

    public MonsterSet getMonsterSet() {
        return monsterSet;
    }

    public void setMonsterSet(MonsterSet monsterSet) {
        this.monsterSet = monsterSet;
    }

    public void initMonster() {
        wolf1.setId("Monster1");
        wolf1.setDescription("狼怪1");
        wolf1.setHpValue(100);
        wolf1.setCurrentWeapon(new Knife("刀", "刀", -40));

        wolf2.setId("Monster2");
        wolf2.setDescription("狼怪2");
        wolf2.setHpValue(100);
        wolf2.setCurrentWeapon(new Knife("刀", "刀", -40));

        Gnome1.setId("Monster3");
        Gnome1.setDescription("地精1");
        Gnome1.setHpValue(70);
        Gnome1.setCurrentWeapon(new BowandArrow("弓箭", "丛林弓", -30));

        Gnome2.setId("Monster4");
        Gnome2.setDescription("地精2");
        Gnome2.setHpValue(70);
        Gnome2.setCurrentWeapon(new BowandArrow("弓箭", "丛林弓", -30));

        Skeleton.setId("Monster5");
        Skeleton.setDescription("骷髅怪");
        Skeleton.setHpValue(75);
        Skeleton.setCurrentWeapon(new BowandArrow("弓箭", "普通弓", -35));

        monsterSet.getMonsters().add(wolf1);
        monsterSet.getMonsters().add(wolf2);
        monsterSet.getMonsters().add(Gnome1);
        monsterSet.getMonsters().add(Gnome2);
        monsterSet.getMonsters().add(Skeleton);
    }

    public void initMonsterPlace() {
        house = new Place("小屋");
        forest = new Place("森林");
        plain = new Place("平原");
        castle = new Place("城堡");
        marsh = new Place("沼泽");

        house.setExit("west", plain);
        house.setExit("east", forest);
        house.setExit("south", marsh);

        forest.setExit("south", marsh);
        forest.setExit("west", house);
        forest.setExit("north", plain);

        plain.setExit("south", forest);
        plain.setExit("east", house);
        plain.setExit("north", marsh);

        marsh.setExit("north", house);
        marsh.setExit("south", plain);

        wolf1.setCurrentPlace(house);
        wolf2.setCurrentPlace(forest);
        Gnome1.setCurrentPlace(plain);
        Gnome2.setCurrentPlace(castle);
        Skeleton.setCurrentPlace(marsh);
    }

    public void judgeMonsters(ArrayList<Monster> monsters) {
        Iterator<Monster> iterator = monsters.iterator();
        while (iterator.hasNext()) {
            Monster i = iterator.next();
            if (i.getHpValue() <= 0) {
                iterator.remove();
            }
        }
    }

    public void judgeGame(Sense sense) {
        if (sense.getMonsterSet().getMonsters().size() == 0) {
            System.out.println("胜利");
            System.exit(0);
        } else if (sense.getPerson().getHpValue() <= 0) {
            System.out.println("失败");
            System.exit(0);
        } else {
            ui.displayStaus(this);
            initMonsterPlace();
        }
    }

    public void play() {
        Scanner in = new Scanner(System.in);
        initMonster();
        initMonsterPlace();
        initPerson();
        initPlace();
        ArrayList<Monster> availableMonsters = new ArrayList<Monster>();
        ArrayList<Monster> dangerousMonsters=new ArrayList<Monster>();
        int flag;
        ui.displayHelpMsg();
        ui.displayStaus(this);
        while (true) {
            String cmd = in.nextLine();
            String[] words = cmd.split(" ");

            if (words[0].equals("bye")) {
                ui.displayBye();
                System.exit(0);
            } else if (words[0].equals("help")) {
                ui.displayHelpMsg();

            } else if (words[0].equals("chop")) {
                for (Monster item : this.getMonsterSet().getMonsters()) {
                    if (item.getCurrentPlace().getDescription().equals(this.getPerson().getCurrentPlace().getDescription())) {
                        availableMonsters.add(item);
                    } else {
                    }
                }
                flag = 0;

                for (Monster i : availableMonsters
                ) {
                    if (i == this.getMonsterSet().get(words[1])) {
                        this.getPerson().useArticle(this.getPerson().getCurrentWeapon(), this.getMonsterSet().get(words[1]));
                        ui.displayDamageMsg(this.getPerson(), this.getMonsterSet().get(words[1]));
                        flag = 1;
                    } else {

                    }
                }
            }
            judgeMonsters(monsterSet.getMonsters());

            for (Monster item:this.getMonsterSet().getMonsters()
                 ) {
                if (item.getCurrentPlace().getDescription().equals(this.getPerson().getCurrentPlace().getDescription())){
                    dangerousMonsters.add(item);
                }
                else {

                }

            }
            for (Monster i:dangerousMonsters
                 ) {
                if (1 == Utils.randomMonsterChop()){
                    i.useArticle(i.getCurrentWeapon(), this.getPerson());
                    ui.displayDamageMsg(i, this.getPerson());
                } else {

                }
            }
            availableMonsters.clear();
            dangerousMonsters.clear();

                    if (words[0].equals("go")) {
                        this.move(this.person, words[1]);
                    }
                    if (words[0].equals("stay")) {
                        System.out.println("你留在了该场景");
                    }

                    judgeGame(this);


        }
    }
}












